﻿@using CodeGenerator.Core
@using System.Text
@{
    int count = 0;
    string primaryKey = "";
    TableInfo table = Model.TableInfo;
    string mName = CodeGenerator.Common.Utils.Pascal(Model.Name);   
}
using NHibernate;
using Shengyi.Common;
using Shengyi.Kefu_ViewModel.Domain.SYWeb;
using Shengyi.Model.Domain;
using Shengyi.Model.Domain.SYWeb;
using Shengyi.OA.BLL;
using Shengyi.OA.WebCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shengyi.Kefu_WEB.Kefu_BLL
{
    public class @(mName)Logic
    {
        #region GetList
        /// <summary>
        /// 获取列表
        /// </summary>
        public IList<@(mName)ListVM> Get@(mName)List(HtmlViewCondition htmlView, @(mName)ListSearchVM search, out int totalSize)
        {
            var list = DataAccess.Context.@(mName)Dao.Get@(mName)List(htmlView, search, out totalSize);
            IList<@(mName)ListVM> result = List_@(mName)Model2List_@(mName)ListVM(list);
            return result;
        }

        /// <summary>
        /// @(mName)Model 转 @(mName)ListVM
        /// </summary>
        private IList<@(mName)ListVM> List_@(mName)Model2List_@(mName)ListVM(IList<@(mName)Model> list)
        {
            List<@(mName)ListVM> returnList = new List<@(mName)ListVM>();

            if (list.IsAny())
            {
                var createUserList = DataAccess.Context.UserManageMentDao.GetAccountInfoList(list.Select(d => d.CreateUserId).ToList());
                var updateUserList = DataAccess.Context.UserManageMentDao.GetAccountInfoList(list.Select(d => d.UpdateUserId).ToList());

                foreach (var item in list)
                {
                    @(mName)ListVM vm = new @(mName)ListVM();
                    BaseCommon.MergeModel(ref vm, item);
                    vm.CreateUserName = createUserList.Where(d => d.Id == item.CreateUserId).Select(d => d.Name).FirstOrDefault().ToStringNotNull();
                    vm.UpdateUserName = updateUserList.Where(d => d.Id == item.UpdateUserId).Select(d => d.Name).FirstOrDefault().ToStringNotNull();

                    //TODO:需要添加其他转换逻辑
                    returnList.Add(vm);
                }
            }
            return returnList;
        } 
        #endregion GetList

        #region Save
        /// <summary>
        /// 保存
        /// </summary>
        public OAResponseModel Save@(mName)(Edit@(mName)VM vm)
        {
            var result = new OAResponseModel();
            #region 非空验证
            @foreach (ColumnInfo column in table.ColumnList)
            {
                if(column.IsPrimaryKey == "否")
                {
                    if(column.AllowDBNull=="否")        
                    {
            @:if(vm.@(column.Name).IsNullOrAll())
                @:return result.SetUnValidParameters("@(column.Comments)不能为空");
                    }
                }
            }
            #endregion 非空验证

            #region 其他验证
            //TODO:需要添加其他验证
            #endregion 其他验证
            
            @(mName)Model model = DataAccess.Context.@(mName)Dao.Get@(mName)Model(d => d.Id == vm.Id && d.IsDelete);
            if (model.IsNull())
            {//新增
                model = new @(mName)Model();
                vm.Id = Guid.NewGuid();
                vm.CreateTime = DateTime.Now;
                vm.CreateUserId = AccountUtility.CurrnetUserId;
                vm.IsDelete = false;
                
                //TODO:需要添加新增的默认参数
            }
            else
            {
                vm.CreateTime = model.CreateTime;
                vm.CreateUserId = model.CreateUserId;                
                vm.IsDelete = model.IsDelete;

                //TODO:需要添加修改中不要修改得内容
            }

            vm.UpdateTime = DateTime.Now;
            vm.UpdateUserId = AccountUtility.CurrnetUserId;
            BaseCommon.MergeModel(ref model, vm);

            using (ITransaction trans = DataAccess.Context.CommonDao.BeginTransaction())
            {
                try
                {
                    DataAccess.Context.CommonDao.SaveTrans(model);
                    trans.Commit();
                    return result.SetSuccessfulParameters(model.Id);
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    return result.SetUnValidParameters(ex.Message);
                }
            }
        }
        #endregion Save

        #region GetModel
        /// <summary>
        /// GetModel
        /// </summary>
        public OAResponseModel Get@(mName)Model(Guid id)
        {
            var result = new OAResponseModel();
            var model = DataAccess.Context.@(mName)Dao.Get@(mName)Model(d => d.Id == id);
            if (model.IsNull())
            {
                return result.SetUnValidParameters();
            }
            var vm = @(mName)Model2Edit@(mName)VM(model);    
            return result.SetSuccessfulParameters(vm);
        }

        /// <summary>
        /// @(mName)Model 转 Edit@(mName)VM
        /// </summary>
        private Edit@(mName)VM @(mName)Model2Edit@(mName)VM(@(mName)Model model)
        {
            Edit@(mName)VM result=new Edit@(mName)VM();
            BaseCommon.MergeModel(ref result, model);

            var createUser=DataAccess.Context.UserManageMentDao.GetAccountInfo(model.CreateUserId);
            var updateUser=DataAccess.Context.UserManageMentDao.GetAccountInfo(model.UpdateUserId);
            result.CreateUserName = createUser.IsNull()?ShengYiTip.NotExistAccount:createUser.Name;
            result.UpdateUserName = updateUser.IsNull()?ShengYiTip.NotExistAccount:updateUser.Name;
            //TODO:需要添加其他转换内容

            return result;
        }
        #endregion GetModel 

        #region Delete
        /// <summary>
        /// 假删
        /// </summary>
        public OAResponseModel Delete@(mName)(Guid id, bool IsDelete = true)
        {
            var result = new OAResponseModel();
            var model = DataAccess.Context.@(mName)Dao.Get@(mName)Model(d => d.Id == id);
            if (model.IsNull())
            {
                return result.SetUnValidParameters();
            }
            model.IsDelete = IsDelete;
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = AccountUtility.CurrnetUserId;
            DataAccess.Context.CommonDao.Save(model);
            return result.SetSuccessfulParameters(model.Id);
        }

        /// <summary>
        /// 真删
        /// </summary>
        public OAResponseModel RealDelete@(mName)(Guid id)
        {
            var result = new OAResponseModel();
            var model = DataAccess.Context.@(mName)Dao.Get@(mName)Model(d => d.Id == id);
            if (model.IsNull())
            {
                return result.SetUnValidParameters();
            }
            DataAccess.Context.CommonDao.Delete(model);
            return result.SetSuccessfulParameters(model.Id);
        }
        #endregion Delete
    }
}